﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SWCad
{
    public partial class TLJSChart : UserControl
    {
        public ClassLib clib = new ClassLib();

        String InFileName, OutFileName;
        String[] BatchFileNames;
        enum iInMode:int {iNo=0, iSingle = 1, iBatch=2}; //0 no input; 1 single input; 2 batch input
        int iInFileMode=(int)iInMode.iNo;
        enum iMValueMode : int { iCustom = 0, iFJSea = 1, iFJLand = 2 }; //0 给定值; 1 福建沿海; 2 福建内地
        int iMValueMethod = (int)iMValueMode.iCustom;
        double dblSMCstm; //给定值
        bool bHasDrawChart = false;
        public TLJSChart()
        {
            InitializeComponent();
        }
        public void getInPutValue(ClassLib clib)
        {
        //    textBoxCSName.Text = clib.strBZM;
            textBoxRainSharp.Text = clib.strYX;
            textBoxArea.Text = clib.dblF.ToString();
            textBoxRiverLength.Text = clib.dblHL.ToString();
            textBoxJ.Text = clib.dblBJ.ToString();
            textBoxZB.Text = clib.iZB.ToString();
            textBoxSm.Text = clib.dblSm.ToString();
            textBoxFc.Text = clib.dblFc.ToString();
            textBoxMp.Text = clib.iMp.ToString();
            textBoxM.Text = clib.iM.ToString();

            if (checkedListBoxPL.Items.Count > 0)
            {
                checkedListBoxPL.Items.Clear();
            }
            for(int i= 0; i<clib.iMp; i++)
            {
                checkedListBoxPL.Items.Add("P="+clib.dblPP[i]+"%");
            }
            checkedListBoxPL.SetItemChecked(0, true);
        }
        private void setInPutValue(ClassLib clib)
        {
            //    textBoxCSName.Text = clib.strBZM;
            clib.strYX = textBoxRainSharp.Text;
            clib.dblF = double.Parse(textBoxArea.Text);
            clib.dblHL = double.Parse(textBoxRiverLength.Text);
            clib.dblBJ = double.Parse(textBoxJ.Text);
            clib.iZB = int.Parse(textBoxZB.Text);
            clib.dblSm = double.Parse(textBoxSm.Text);
            clib.dblFc = double.Parse(textBoxFc.Text);
            clib.iMp = int.Parse(textBoxMp.Text);
            clib.iM = int.Parse(textBoxM.Text);

//             if (checkedListBoxPL.Items.Count > 0)
//             {
//                 checkedListBoxPL.Items.Clear();
//             }
//             for (int i = 0; i < clib.iMp; i++)
//             {
//                 checkedListBoxPL.Items.Add("P=" + clib.dblPP[i] + "%");
//             }
//             checkedListBoxPL.SetItemChecked(0, true);
        }
        //由输入文件计算图标显示所需数据
        private void CalChart()
        {
            String ConnectFile = InFileName + "*" + OutFileName + "*";
            //获取字节个数，传递给DLL中的函数
            byte[] bit = Encoding.Default.GetBytes(ConnectFile);
            Int32 InLen = bit.Length;
            //           c.testWTSTR(unicodeString, strLength);
            clib.readTLJS(InFileName);
            clib.TLJS(ConnectFile, InLen); //计算
            clib.readTLJSouttemp(OutFileName + "temp");
            getInPutValue(clib);
        }
        //添加控制断面
        private void AddCSName()
        {
            cmbCSNames.Items.Add(clib.strBZM);
            cmbCSNames.SelectedIndex = cmbCSNames.Items.Count - 1;
        }
        //绘图
        private void DrawChart()
        {
            // Check if a demo module node is selected. Demo module is attached to the node
            // as the Tag property
            clib.TLJS2Plot();
            LineChart LineChart = new LineChart(clib);
            LineChart.Dock = DockStyle.Fill;
            LineChart.Name = "LineChart";
            if(ChartControl.Controls.Count>0)
                ChartControl.Controls[0].Dispose(); //刷新图
            ChartControl.Controls.Add(LineChart);
            bHasDrawChart = true;
        }
  
        private void importToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Initialize the OpenFileDialog to look for text files:
            BatchAnalyseDlg.Filter = "InPut Files|*.dat";
            // Check if the user selected a file from the OpenFileDialog:
            if (BatchAnalyseDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BatchFileNames = BatchAnalyseDlg.FileNames;
                Int32 iFileNum = BatchFileNames.Length;
                cmbCSNames.Items.Clear();
                for (int i = 0; i < iFileNum; i++)
                {
                    //根据批量输入文件名生成批量输出文件名
                    InFileName = BatchFileNames[i];
                    OutFileName = InFileName.Remove(InFileName.Length - 4) + ".out";
                    CalChart();
                    AddCSName();
                }
                iInFileMode = (int)iInMode.iBatch;
            }
            InitMValueCmb();
            updateMenu();
            updateItem();
        }

        //选择绘制某个频率的成果
        private void checkedListBoxPL_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBoxPL.Items.Count; i++)
            {
                checkedListBoxPL.SetItemChecked(i, false);
            }
            if (checkedListBoxPL.CheckedItems == null)
            {
                checkedListBoxPL.SetItemChecked(checkedListBoxPL.SelectedIndex, false);
            }
            else
            {
                checkedListBoxPL.SetItemChecked(checkedListBoxPL.SelectedIndex, true);
                clib.iPLShow = checkedListBoxPL.SelectedIndex;
            }
            DrawChart();            
        }
        private void ChartControl_MouseMoveEvent(System.Object eventSender, AxXtremeChartControl._DChartControlEvents_MouseMoveEvent eventArgs)
        {
            XtremeChartControl.ChartElement Element;
            Element = ChartControl.HitTest(eventArgs.x, eventArgs.y);

            XtremeChartControl.ChartSeriesPoint Point;

            Point = (XtremeChartControl.ChartSeriesPoint)Element;

            if (Point != null)
            {
                System.Diagnostics.Debug.WriteLine("Point " + Point[0]);
            }
        }

        private void Line_Resize(System.Object eventSender, System.EventArgs eventArgs)
        {
 //           menuStrip1.Width = ClientRectangle.Width - 2 * gbOptions.Left;
 //           gbOptions.Width = ClientRectangle.Width - 2 * gbOptions.Left;
            ChartControl.SetBounds(ChartControl.Left, ChartControl.Top, ClientRectangle.Width - 2 * ChartControl.Left+ChartControl.Left, ClientRectangle.Height - ChartControl.Top - 8);
        }

        private void saveChartAsPNGToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                dlgCommonDialogSave.Title = "Save As PNG";
                dlgCommonDialogSave.AutoUpgradeEnabled = true;
                dlgCommonDialogSave.Filter = "Png Files (*.PNG)|*.PNG";
                if (dlgCommonDialogSave.ShowDialog() == DialogResult.OK)
                {
                    if (dlgCommonDialogSave.FileName.Length == 0)
                        return;

                    foreach (object x in ChartControl.Controls[0].Controls)
                    {
                        if (x is AxXtremeChartControl.AxChartControl)
                            ((AxXtremeChartControl.AxChartControl)x).SaveAsImage(dlgCommonDialogSave.FileName, ((AxXtremeChartControl.AxChartControl)x).Width, ((AxXtremeChartControl.AxChartControl)x).Height);
                    }
                }

            }
            catch (Exception es)
            {
                MessageBox.Show(es.Message);
            }
        }

        //选择指定控制断面 2012-1-27
        private void cmbCSNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 i = cmbCSNames.SelectedIndex;
            //根据指定控制断面，刷新参数
            if(iInFileMode == (int)iInMode.iBatch)
                InFileName = BatchFileNames[i];
            OutFileName = InFileName.Remove(InFileName.Length - 4) + ".out";

            CalChart();
            DrawChart();
        }
        private void updateMenu()
        {
            if((int)iInMode.iNo == iInFileMode)
            {
                exportResultToolStripMenuItem.Enabled = false;
            }
            else if ((int)iInMode.iSingle == iInFileMode || (int)iInMode.iBatch == iInFileMode)
            {
                exportResultToolStripMenuItem.Enabled = true;
            }
            saveChartAsPNGToolStripMenuItem.Enabled = bHasDrawChart;
        }
        private void updateItem()
        {
            if (0 == double.Parse(textBoxSm.Text))
            {
                textBoxZB.Enabled = false;
                textBoxSm.Enabled = false;
                cmbSmMtd.Enabled = false;
            }
            else
            {
                textBoxZB.Enabled = true;
                textBoxSm.Enabled = true;
                cmbSmMtd.Enabled = true;
            }
        }
        //输出计算成果汇总表
        //第一行为各计算频率
        //余下为各断面相应频率下得洪峰流量
        private void exportResultToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Initialize the OpenFileDialog to look for text files:
            saveResDlg.Filter = "Export Files|*.txt";
            // 自动追加后缀
            saveResDlg.AddExtension = true; 
            // Check if the user selected a file from the OpenFileDialog:
            if (saveResDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //实例化一个文件流-->与写入文件相关联
                FileStream ofs = new FileStream(saveResDlg.FileName, FileMode.Create);
                StreamWriter sw = new StreamWriter(ofs);
                string buf=null;

                if(iInFileMode == (int)iInMode.iSingle)
                {
                    clib.readTLJSouttemp(OutFileName + "temp");
                    buf = clib.strBZM;
                    sw.WriteLine(buf);
                    //输出计算频率
                    buf=null;
                    for (int iP = 0; iP < clib.iMp; iP++)
                        buf += clib.dblPP[iP] + "%\t";
                    sw.WriteLine(buf);
                    //输出对应频率的洪峰流量
                    buf=null;
                    for (int iP = 0; iP < clib.iMp; iP++)
                        buf += clib.dblQm[iP]+"\t";
                    sw.WriteLine(buf);
                }
                else if (iInFileMode == (int)iInMode.iBatch)
                {
                    Int32 iFileNum = BatchFileNames.Length;
                    for (int i = 0; i < iFileNum; i++)
                    {
                        InFileName = BatchFileNames[i];
                        OutFileName = InFileName.Remove(InFileName.Length - 4) + ".out";
                        clib.readTLJSouttemp(OutFileName + "temp");
                        cmbCSNames.SelectedIndex = i;                       
                        
                        //第一行输出计算频率
                        buf = null;
                        if(0 == i)
                        {
                            buf = "控制断面\t";
                            for (int iP = 0; iP < clib.iMp; iP++)
                                buf += clib.dblPP[iP] + "%\t";
                            sw.WriteLine(buf);
                        }

                        //输出对应频率的洪峰流量
                        buf = null;
                        buf = cmbCSNames.SelectedItem.ToString() + "\t";
                        for (int iP = 0; iP < clib.iMp; iP++)
                            buf += clib.dblQm[iP]+"\t";
                        sw.WriteLine(buf);
                    }
                }
                sw.Close();
                ofs.Close();
            }
        }

        private void btn_CalNew_Click(object sender, EventArgs e) //刷新绘图区
        {
            setInPutValue(clib);
            clib.WriteTLJS(InFileName);
            CalChart();
            DrawChart();
        }
        //添加m值计算方法类型
        private void InitMValueCmb()
        {
            cmbSmMtd.Items.Clear();
            cmbSmMtd.Items.Add("给定m值");
            cmbSmMtd.Items.Add("福建沿海区");
            cmbSmMtd.Items.Add("福建内地区");
            cmbSmMtd.SelectedIndex = 0;
            clib.readTLJS(InFileName); //若不加这句，clib.dblsm变成零？？
            dblSMCstm = clib.dblSm;
            getInPutValue(clib);
        }
        private void cmbSmMtd_SelectedIndexChanged(object sender, EventArgs e) //选择m值计算方法
        {
            iMValueMethod = cmbSmMtd.SelectedIndex;
            setInPutValue(clib); //更新河长、坡降、面积
            //根据指定m值计算方法，计算m值
            if (iMValueMethod == (int)iMValueMode.iCustom)
                clib.dblSm = dblSMCstm;
            else if (iMValueMethod == (int)iMValueMode.iFJSea)
            {
                double theta = clib.dblHL / Math.Pow(clib.dblBJ,1/3)/Math.Pow(clib.dblF,0.25);
                if (theta >= 2.5)
                    clib.dblSm = 0.053 * Math.Pow(theta, 0.785) * 10;
                else if (theta < 2.5)
                    clib.dblSm = 0.062 * Math.Pow(theta, 0.613) * 10;
            }
            else if (iMValueMethod == (int)iMValueMode.iFJLand)
            {
                double theta = clib.dblHL / Math.Pow(clib.dblBJ, 1 / 3) / Math.Pow(clib.dblF, 0.25);
                if (theta >= 2.5)
                    clib.dblSm = 0.035 * Math.Pow(theta, 0.785) * 10;
                else if (theta < 2.5)
                    clib.dblSm = 0.041 * Math.Pow(theta, 0.613) * 10;
            }
            getInPutValue(clib); //clib类m值赋给窗口 
        }

        private void HyetographToolStripMenuItem_Click(object sender, EventArgs e) //编辑雨型
        {
            FormHyetograph fmHtGp = new FormHyetograph(); //实例化子窗口
            fmHtGp.ShowDialog(clib.strYX,this,clib);//子窗口接收          
        }

        private void MaxPPToolStripMenuItem_Click(object sender, EventArgs e) //编辑各频率短历时暴雨
        {
            FormMaxPP fmMaxPP = new FormMaxPP(); //实例化子窗口
            fmMaxPP.ShowDialog(clib.strBZM, this, clib);//子窗口接收   
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void btnMValueRef_Click(object sender, EventArgs e)
        {
            FormMValue fmMValue = new FormMValue(); //实例化子窗口
            fmMValue.ShowDialog();//子窗口接收    
        }
    }
}
